<template>
  <div id="login">
    <!-- 卡片组件 -->
    <el-card class='login-card'>
      <!-- 登录表单 -->
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" style="margin-top: 50px">
        <el-form-item prop="mobile">
          <el-input v-model="loginForm.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
 

<script>
import Cookies from 'js-cookie';

export default {
  name: 'App',
  data() {
    // 自定义校验规则
    // const checkMobile = (rule, value, callback) => {
    //   // 校验的是 value 第三位必须是 9
    //   value.charAt(2) === "9" ? callback() : callback(new Error('手机号第三位必须是9'))
    // }
    return {
      // 数据对象
      loginForm: {
        //  校验的字段
        mobile: '15671660000',
        password: 'abcdefg'
      },
      // 放置的是校验的规则
      loginRules: {
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }, {
          min: 6, max: 16, message: '密码应为6-16位的长度', trigger: 'blur'
        }]
      },
    }
  },
  methods: {
    generateRoutes(authLis) {
      if (!authLis) {
        return [];
      }
      let res = []
      authLis.map((item) => {
        res.push({
          path: item.path,
          name: item.name,
          component: () => import(/* webpackChunkName: "course" */ '../components/' + item.componentName),
          children: this.getChildRoute(item.children)
        })
      })
      return res;
    },
    getChildRoute(children) {
      if (!children) {
        return [];
      }
      let childRoutes = []
      children.map((item) => {
        childRoutes.push({
          path: item.path,
          name: item.name,
          component: () => import(/* webpackChunkName: "course" */ '../components/' + item.componentName),
        })
      })
      return childRoutes;
    },
    getData() {
      //定义常量保存 url
      const url = "https://vue.ruoyi.vip/prod-api/login";
      //发送请求
      this.axios
        .post(url, {
          //携带参数
          params: {
            username: this.user.username,
            password: this.user.password,
          },
        })
        .then((res) => {
          console.log(res.data);
          alert("登录成功")
          //成功就将对话框关闭
          this.dialogFormVisible = false;
          //跳转页面,前端跳转页面必须使用路由,使用$router对象中的push方法
          this.$router.push('/index');
        })
        .catch((error) => {
          console.log(error);
          //出现错误使用ElementUI提供的消息提示
          this.$message.error("对不起! 登录错误");
        });
    },
    login() {
      this.$refs.loginForm.validate(isOk => {
        if (isOk) {
          // 表示 校验通过
          console.log('校验通过')
          // 调用登录方法。。。
          // 这里模拟登录成功以后
          const user = {
            token: 'osa394823403290gde3383333',
            id: '17888',
            auth: [
              {
                url: '',
                name: '系统管理',
                code: '1001',
                children: [
                  {
                    url: '/system/user',
                    name: '用户管理',
                    code: '1001001',
                  },
                  {
                    url: '/system/role',
                    name: '角色管理',
                    code: '1001002'
                  },
                  {
                    url: '/system/menu',
                    name: '菜单管理',
                    code: '1001003'
                  },
                  {
                    url: '/system/dict',
                    name: '字典管理',
                    code: '1001004'
                  }
                ]
              },
              {
                url: "",
                name: '系统工具管理',
                code: "1002",
                children: [
                  {
                    url: '/system/form',
                    name: '表单管理',
                    code: '1002001'
                  },
                ]
              },
              {
                url: "",
                name: '系统监控管理',
                code: "1003",
                children: [
                  {
                    url: '/system/monitor',
                    name: '定时任务管理',
                    code: '1003001'
                  },
                ]
              }

            ]
          }
          this.$store.commit('SET_TOKEN', user.token)
          this.$store.commit('SET_ID', user.id)
          this.$store.commit('SET_PERMISSIONS', user.auth)
          Cookies.set("_PERMISSIONS", JSON.stringify(user.auth))
          console.log(this.$store.state.user, user)
          localStorage.setItem('username', 'john_doe');
          this.$router.push('/home');

        }
      })
    }


  }
}
</script>
 
<style>
#login {
  width: 100%;
  height: 100vh;
  background-color: #7a51ae;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}

.login-card {
  width: 440px;
  height: 300px;
}
</style>